'use strict';
const axios = require('axios');
const https = require('https');
const agent = new https.Agent({
  rejectUnauthorized: false,
});

axios.interceptors.response.use(function(response) {
  // 对响应数据做点什么
  return response;
}, function(error) {
  // 对响应错误做点什么
  if (error.response.status === 401) {
    console.log('发现401:');
    const data = {
      code: 401,
      message: 'login timeout',
    };
    return Promise.resolve(data);
  }
  return Promise.reject(error);
});

axios.interceptors.request.use(function(config) {
  config.httpsAgent = agent;
  config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  return config;
}, function(error) {
  // Do something with request error
  return Promise.reject(error);
});

exports.axios = axios;
